package com.lyu.travel.controller;

import com.lyu.travel.core.entity.User;
import com.lyu.travel.core.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;

/**
 * @ProjectName: [travel]
 * @Package: [com.lyu.travel.controller]
 * @ClassName: [LoginController]
 * @Description: 登录控制器
 * @Author: [78483]
 * @CreateDate: [2020-06-04 21:56]
 * @UpdateUser: [78483]
 * @UpdateDate: [2020-06-04 21:56]
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 */
@Controller
public class LoginController {
    @Autowired
    UserMapper userMapper;


    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
        return "login";
    }

    //获取登录请求
    @PostMapping("/login")
    public String login(String username, String passwd, Model model, Map<String, Object> map, HttpSession session) throws IOException {

        User user = userMapper.findByUsernameAndPassword(username,passwd);
        if(user == null || (username=="" && passwd == "")){
            model.addAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
            map.put("msg", "登录失败,请检查用户名和密码");
            return "login";

        }else {
            model.addAttribute("username", username);
            model.addAttribute("passwd", passwd);
            session.setAttribute("user", username);
            return "dashboard";
        }
    }


    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "login";

    }


    @GetMapping("/welcome")
    public String welcome(Model model) {

        return "dashboard";
    }

}
